// Copyright(C) 2025 InfiniFlow, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export module infinity_core:kv_utility;

import :status;

import internal_types;

namespace infinity {

class KVInstance;
class IndexBase;

export std::shared_ptr<std::vector<SegmentID>> GetTableSegments(KVInstance *kv_instance,
                                                                const std::string &db_id_str,
                                                                const std::string &table_id_str,
                                                                TxnTimeStamp begin_ts,
                                                                TxnTimeStamp commit_ts);

export std::vector<SegmentID> GetTableIndexSegments(KVInstance *kv_instance,
                                                    const std::string &db_id_str,
                                                    const std::string &table_id_str,
                                                    const std::string &index_id_str,
                                                    TxnTimeStamp begin_ts,
                                                    TxnTimeStamp commit_ts);

export std::vector<BlockID> GetTableSegmentBlocks(KVInstance *kv_instance,
                                                  const std::string &db_id_str,
                                                  const std::string &table_id_str,
                                                  SegmentID segment_id,
                                                  TxnTimeStamp begin_ts,
                                                  TxnTimeStamp commit_ts);

export std::shared_ptr<IndexBase> GetTableIndexDef(KVInstance *kv_instance,
                                                   const std::string &db_id_str,
                                                   const std::string &table_id_str,
                                                   const std::string &index_id_str,
                                                   TxnTimeStamp begin_ts);

export size_t GetBlockRowCount(KVInstance *kv_instance,
                               const std::string &db_id_str,
                               const std::string &table_id_str,
                               SegmentID segment_id,
                               BlockID block_id,
                               TxnTimeStamp begin_ts,
                               TxnTimeStamp commit_ts);

export size_t GetSegmentRowCount(KVInstance *kv_instance,
                                 const std::string &db_id_str,
                                 const std::string &table_id_str,
                                 SegmentID segment_id,
                                 TxnTimeStamp begin_ts,
                                 TxnTimeStamp commit_ts);

export std::string GetLastPartOfKey(const std::string &key, char delimiter = '|');

export u64 GetTimestampFromKey(const std::string &key);

} // namespace infinity
